<?php

namespace app\api\model\scenic;

use app\api\model\scenic\ScenicCategory;
use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Scenic extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'scenic';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'category_id_text',
        'level_text'
    ];

    public function getLevelList()
    {
        return [1=>'1A',2=>'2A',3=>'3A',4=>'4A',5=>'5A'];
    }

    public function getLevelTextAttr($value, $data)
    {
        $value = $value ?: ($data['level'] ?? '');
        $list = $this->getLevelList();
        return $list[$value] ?? '';
    }


    public function getCategoryIdTextAttr($value, $data)
    {
        $value = $value ?: ($data['category_id'] ?? '');

        $category_list = ScenicCategory::where('id','in',$value)->column('name');
        if ($category_list){
            return $category_list;
        } else {
            return '';
        }

    }



    //新增景区
    public function add($params)
    {
        Db::startTrans();

        try {
            $community = json_decode($params['community'],true)?json_decode($params['community'],true):_json_decode($params['community']);

            $media_json = json_decode($params['media_json'],true)?json_decode($params['media_json'],true):_json_decode($params['media_json']);

            $tickets_json = json_decode($params['tickets_json'],true)?json_decode($params['tickets_json'],true):_json_decode($params['tickets_json']);

            $scenic_model = new Scenic();

            $data = [
                'user_id'=>$params['user_id'],
                'name'=>$params['name'],
                'province'=>$params['province'],
                'province_id'=>$params['province_id'],
                'city'=>$params['city'],
                'city_id'=>$params['city_id'],
                'district'=>$params['district'],
                'district_id'=>$params['district_id'],
                'address'=>$params['address'],
                'lat'=>$params['lat'],
                'lng'=>$params['lng'],
                'image'=>$params['image'],
                'open_time'=>$params['open_time'],
                'everyday_open_time'=>$params['everyday_open_time'],
                'rest_time'=>$params['rest_time'],
                'url'=>$params['url'],
                'kefu_tel'=>$params['kefu_tel'],
                'level'=>$params['level'],
                'category_id'=>$params['category_id'],
                'intro'=>$params['intro'],
                'community'=>$community?json_encode($community,320):'',
            ];
            if (empty($data['name'])){
                throw new \Exception('请输入景区名称');
            }
            if (empty($data['province'])||empty($data['city'])||empty($data['district'])){
                throw new \Exception('请选择景区地址');
            }
            if (empty($data['address'])){
                throw new \Exception('请选择园区地址');
            }
            if (empty($data['open_time'])){
                throw new \Exception('请输入开放时间');
            }
            if (empty($data['everyday_open_time'])){
                throw new \Exception('请输入每日开放时间');
            }
            /*if (empty($data['rest_time'])){
                throw new \Exception('请输入休园时间');
            }*/
            if (empty($data['url'])){
                throw new \Exception('请输入景区网址');
            }
            if (empty($data['kefu_tel'])){
                throw new \Exception('请输入客服电话');
            }

            $res = $scenic_model->save($data);
            if ($res) {
                $media_model = new ScenicMedia();
                $media_list = [];
                foreach ($media_json as $k=>$v){
                    $media_list[] = [
                        'scenic_id'=>$scenic_model->scenic_id,
                        'category_id'=>$v['category_id'],
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                $media_model->saveAll($media_list);

                $tickets_model = new ScenicTickets();
                $tickets_list = [];
                foreach ($tickets_json as $k=>$v){
                    $tickets_list[] = [
                        'scenic_id'=>$scenic_model->scenic_id,
                        'type'=>$v['type'],
                        'icon'=>$v['icon'],
                        'price'=>$v['price'],
                        'explain'=>$v['explain'],
                    ];
                }
                $tickets_model->saveAll($tickets_list);
            }
            Db::commit();
            return $res;
        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \Exception($e->getMessage().'-'.$e->getLine());
        }

    }



}
